import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * author: hyacinth
 * Date: 2023/5/27
 */
@WebServlet("/register")
public class registerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setContentType("text/html; charset=utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username.equals("")||password.equals("")){
            resp.sendRedirect("register.html");
            return;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        UserDao userDao = new UserDao();
        try {
            int ret = userDao.insertUser(user);
            if (ret == 1){
                resp.getWriter().write("注册成功!");
            }else{
                resp.getWriter().write("注册失败!");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
